import Vue from 'vue'
import Router from 'vue-router'
import Home from '../views/Home'
// vueRouter 2.几的版本的有个bug一直点击跳转路由会报错
const originalPush = Router.prototype.push;
Router.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
}
Vue.use(Router)

const routes=[
  {
    path: '/',
    redirect: "/Home"
  },
  {
    path: '/Home',
    name: 'Home',
    component: Home,
    meta: {
      activeNum: 0,
      isShow:false,
      isShowTab:true
    },
    children: [
      {
        path: '/Home/SearchPopup',
        name: 'SearchPopup',
        component: () => import("../views/SearchPopup")
      }
    ]
  },
  {
    path: '/Special',
    name: 'Special',
    meta: {
      activeNum: 1,
      isShowTab:true
    },
    component: () => import("../views/Special")
  },
  {
    path: '/CassifyPage',
    name: 'CassifyPage',
    meta: {
      activeNum: 2,
      isShowTab:true
    },
    component: () => import("../views/CassifyPage")
  },
  {
    path: '/Shopping',
    name: 'Shopping',
    meta: {
      activeNum: 3,
      isShowTab:true
    },
    component: () => import("../views/Shopping")
  },
  {
    path: '/MyPage',
    name: 'MyPage',
  
    component: () => import("../views/MyPage")
  },
  {
    path: '/Homepruduct',
    name: 'Homepruduct',
    component: () => import("../views/Homepruduct")
  },
  {
    path: '/Mylogin',
    name: 'Mylogin',
    meta: {
      activeNum: 4,
      isShowTab:true
    },
    component: () => import("../views/MyLogin")
  },
  {
    path: '/Productdetail',
    name: 'Productdetail',
    component: () => import("../views/ProductDetail")
  }
]

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局守卫
router.beforeEach((to, from, next) => {
  let vue = Vue;
  if (to.path == '/Shopping') {
    if (!localStorage.getItem('token')) {
      setTimeout(() => {
        vue.prototype.$toast('没有登陆');
        next('/MyPage')
      },1000)
    } else {
      next()
    }
  }
  if (to.path == '/MyPage') {
    if (localStorage.getItem('token')) {
         next('/Mylogin')
    }
 }

  next()
});
export default router